package com.yf.controller.admin.article;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import com.yf.consts.DaoConst;
import com.yf.entity.ArticleEntity;
import com.yf.response.R;
import com.yf.service.article.ArticleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 文章Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Aug 21 11:02:51 CST 2018
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 列表文章
     */
    @RequestMapping("/list")
    @RequiresPermissions("article:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageHelper.startPage(DaoConst.getPage(params),DaoConst.getLIMIT(params));
        List<ArticleEntity> articleList = articleService.queryList(params);
        return R.ok(new PageInfo(articleList));
    }

    /**
     * 查看文章
     */
    @SysLog("查看文章")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("article:info")
    public R info(@PathVariable("id") Long id) {
        ArticleEntity article = articleService.get(id);

        return R.ok(article);
    }

    /**
     * 保存文章
     */
    @SysLog("保存文章")
    @RequestMapping("/save")
    @RequiresPermissions("article:save")
    public R save(ArticleEntity article) {
        articleService.save(article);

        return R.ok(article);
    }

    /**
     * 修改文章
     */
    @SysLog("修改文章")
    @RequestMapping("/update")
    @RequiresPermissions("article:update")
    public R update(ArticleEntity article) {
        articleService.update(article);

        return R.ok(article);
    }

    /**
     * 删除文章
     */
    @SysLog("删除文章")
    @RequestMapping("/delete")
    @RequiresPermissions("article:delete")
    public R delete(@RequestBody Long[]ids) {
        articleService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有文章
     */
    @SysLog("所有文章")
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ArticleEntity> articleList = articleService.queryList(params);

        return R.ok(articleList);
    }

    /***********自定义开始***********/
}
